home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume3 / totri < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  6.9 KB

  1. Path: xanth!mcnc!rutgers!cmcl2!husc6!necntc!ncoast!allbery
  2. From: jpn@teddy.UUCP (John P. Nelson)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i048: Trigraph converter
  5. Message-ID: <4846@teddy.UUCP>
  6. Date: 10 Jun 88 18:38:54 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: jpn@teddy.UUCP (John P. Nelson)
  9. Organization: GenRad, Inc., Concord, Mass.
  10. Lines: 245
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. comp.sources.misc: Volume 3, Issue 48
  14. Submitted-By: "John P. Nelson" <jpn@teddy.UUCP>
  15. Archive-Name: totri
  16.  
  17. #! /bin/sh
  18. # This is a shell archive, meaning:
  19. # 1. Remove everything above the #! /bin/sh line.
  20. # 2. Save the resulting text in a file.
  21. # 3. Execute the file with /bin/sh (not csh) to create the files:
  22. #    README
  23. #    makefile
  24. #    totri.1
  25. #    totri.c
  26. #    untri.1
  27. #    untri.c
  28. # This archive created: Fri Jun 10 14:33:50 1988
  29. export PATH; PATH=/bin:$PATH
  30. echo shar: extracting "'README'" '(1098 characters)'
  31. if test -f 'README'
  32. then
  33.     echo shar: will not over-write existing file "'README'"
  34. else
  35. sed 's/^X//' << \SHAR_EOF > 'README'
  36. XThe DRAFT ANSI C standard specifies that all conforming compilers must
  37. Xsupport "trigraphs" as an alternate means of writing certain characters
  38. X(which are absent from the international ISO 646 repertoire).
  39. XPersonally, I think that trigraphs are UGLY, and absolutely the WRONG
  40. Xway to solve this problem.  Certainly I believe that trigraph processing
  41. Xdoes not belong in the C compiler, but would be better specified as
  42. Xan external facility.
  43. X
  44. XIn any case, since the standard specifies that the conversion from
  45. Xtrigraphs takes place in the very first processing phase, it is
  46. Xeasy to implement trigraphs as a simple preprocessor.  I have written
  47. Xtwo programs:  one that removes (translates) all trigraph sequences
  48. Xinto the more usual ASCII representation, and the second, which
  49. Xdoes the reverse (converts US-ASCII into trigraphs).
  50. X
  51. XI hope someone finds these useful.  I have donated the source code
  52. Xto the public domain.  Both programs are implemented as filters,
  53. Xbut it would be easy to change the interface by rewriting main.
  54. XThe functions that do the work take input and outfile "FILE *"
  55. Xdescriptors.
  56. SHAR_EOF
  57. if test 1098 -ne "`wc -c < 'README'`"
  58. then
  59.     echo shar: error transmitting "'README'" '(should have been 1098 characters)'
  60. fi
  61. fi
  62. echo shar: extracting "'makefile'" '(90 characters)'
  63. if test -f 'makefile'
  64. then
  65.     echo shar: will not over-write existing file "'makefile'"
  66. else
  67. sed 's/^X//' << \SHAR_EOF > 'makefile'
  68. Xall: totri untri
  69. X
  70. Xtotri: totri.c
  71. X    $(CC) -o $@ totri.c
  72. Xuntri: untri.c
  73. X    $(CC) -o $@ untri.c
  74. SHAR_EOF
  75. if test 90 -ne "`wc -c < 'makefile'`"
  76. then
  77.     echo shar: error transmitting "'makefile'" '(should have been 90 characters)'
  78. fi
  79. fi
  80. echo shar: extracting "'totri.1'" '(534 characters)'
  81. if test -f 'totri.1'
  82. then
  83.     echo shar: will not over-write existing file "'totri.1'"
  84. else
  85. sed 's/^X//' << \SHAR_EOF > 'totri.1'
  86. X.TH totri 1   "10-Jun-88 13:59 jpn"
  87. X.SH NAME
  88. Xtotri \- convert a C program to ANSI C trigraphs.
  89. X.SH SYNTAX
  90. Xtotri < input > output
  91. X.SH DESCRIPTION
  92. XThis program converts all characters in the input that are representable
  93. Xby ANSI C three-character trigraph sequences, into trigraphs.  Any existing
  94. Xtrigraph sequences remain, unmodified.
  95. X.PP
  96. XPresumably this program would be used by someone with a non-US ASCII terminal
  97. Xthat represents certain C punctuation characters as language-specific
  98. Xalphabetic characters.
  99. X.SH AUTHOR
  100. XJohn P. Nelson
  101. SHAR_EOF
  102. if test 534 -ne "`wc -c < 'totri.1'`"
  103. then
  104.     echo shar: error transmitting "'totri.1'" '(should have been 534 characters)'
  105. fi
  106. fi
  107. echo shar: extracting "'totri.c'" '(691 characters)'
  108. if test -f 'totri.c'
  109. then
  110.     echo shar: will not over-write existing file "'totri.c'"
  111. else
  112. sed 's/^X//' << \SHAR_EOF > 'totri.c'
  113. X/* totri - convert all "funny" characters to ANSI C trigraph sequences.
  114. X *         currently implemented as a filter, but a rewritten main
  115. X *         could allow a more sophisticated interface.
  116. X *
  117. X *  This source donated to the public domain by John P. Nelson 1988
  118. X */
  119. X
  120. X#include <stdio.h>
  121. X#include <strings.h>
  122. X
  123. Xchar *trichar = "=(/)'<!>-";
  124. Xchar *translate = "#[\\]^{|}~";
  125. Xmain()
  126. X    {
  127. X    process(stdin, stdout);
  128. X    }
  129. X
  130. Xprocess(in, out)
  131. XFILE *in, *out;
  132. X    {
  133. X    int c;
  134. X    char *ptr;
  135. X
  136. X    while ((c = getchar(in)) != EOF)
  137. X    {
  138. X    if (ptr = strchr(translate, c))
  139. X        {
  140. X        putc('?', out);
  141. X        putc('?', out);
  142. X        putc(trichar[ptr - translate], out);
  143. X        }
  144. X    else
  145. X        putc(c, out);
  146. X    }
  147. X    }
  148. SHAR_EOF
  149. if test 691 -ne "`wc -c < 'totri.c'`"
  150. then
  151.     echo shar: error transmitting "'totri.c'" '(should have been 691 characters)'
  152. fi
  153. fi
  154. echo shar: extracting "'untri.1'" '(380 characters)'
  155. if test -f 'untri.1'
  156. then
  157.     echo shar: will not over-write existing file "'untri.1'"
  158. else
  159. sed 's/^X//' << \SHAR_EOF > 'untri.1'
  160. X.TH untri 1   "10-Jun-88 13:54 jpn"
  161. X.SH NAME
  162. Xuntri \- filter to remove ANSI C trigraph sequences.
  163. X.SH SYNTAX
  164. Xuntri < input > output
  165. X.SH DESCRIPTION
  166. XThis program converts the three character trigraph sequences defined
  167. Xby ANSI C into the more usual single character sequences.  All other
  168. Xcharacters are passed through unchanged.
  169. X.SH "SEE ALSO"
  170. Xtotri (1)
  171. X.SH "Author"
  172. XJohn P. Nelson
  173. SHAR_EOF
  174. if test 380 -ne "`wc -c < 'untri.1'`"
  175. then
  176.     echo shar: error transmitting "'untri.1'" '(should have been 380 characters)'
  177. fi
  178. fi
  179. echo shar: extracting "'untri.c'" '(1236 characters)'
  180. if test -f 'untri.c'
  181. then
  182.     echo shar: will not over-write existing file "'untri.c'"
  183. else
  184. sed 's/^X//' << \SHAR_EOF > 'untri.c'
  185. X/* untri - convert ANSI C trigraph sequences to single characters.
  186. X *         currently implemented as a filter, but a rewritten main
  187. X *         could allow a more sophisticated interface.
  188. X *
  189. X *  This source donated to the public domain by John P. Nelson 1988
  190. X */
  191. X
  192. X#include <stdio.h>
  193. X#include <strings.h>
  194. X
  195. Xchar *trichar = "=(/)'<!>-";
  196. Xchar *translate = "#[\\]^{|}~";
  197. Xmain()
  198. X    {
  199. X    process(stdin, stdout);
  200. X    }
  201. X
  202. X/*
  203. X * Note:  I used a goto in this function, because we are essentially
  204. X *     performing a two character lookahead, but unputc is only guaranteed
  205. X *     to be able to push back one character.  Otherwise, the goto would be
  206. X *     unnecessary.
  207. X */
  208. Xprocess(in, out)
  209. XFILE *in, *out;
  210. X    {
  211. X    int c;
  212. X    char *ptr;
  213. X
  214. X    while ((c = getchar(in)) != EOF)
  215. X    {
  216. Xreprocess:
  217. X    if (c == '?')
  218. X        {
  219. X        if ((c = getc(in)) != '?')
  220. X        {
  221. X        if (c != EOF)
  222. X            ungetc(c, in);
  223. X        putc('?', out);
  224. X        continue;
  225. X        }
  226. X        c = getc(in);
  227. X        if (c != EOF)
  228. X        {
  229. X        if (ptr = strchr(trichar, c))
  230. X            {
  231. X            /* yup, it's a trigraph */
  232. X            putc(translate[ptr - trichar], out);
  233. X            continue;
  234. X            }
  235. X        ungetc(c, in);
  236. X        }
  237. X        putc('?', out);
  238. X        c = '?';
  239. X        /* ungetc('?', in); continue; */
  240. X        goto reprocess;
  241. X        }
  242. X    putc(c, out);
  243. X    }
  244. X    }
  245. SHAR_EOF
  246. if test 1236 -ne "`wc -c < 'untri.c'`"
  247. then
  248.     echo shar: error transmitting "'untri.c'" '(should have been 1236 characters)'
  249. fi
  250. fi
  251. exit 0
  252. #    End of shell archive
  253. -- 
  254.      john nelson
  255.  
  256. UUCP:    {decvax,mit-eddie}!genrad!teddy!jpn
  257. smail:    jpn@genrad.com
  258.